package com.zhs.gccs.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zhs.gccs.R;
import com.zhs.gccs.model.TaskProject;
import com.zhs.gccs.utils.OnNoDoubleClickListener;

import java.util.List;

public class ProjectInfoAdapter<T> extends RecyclerView.Adapter<ProjectInfoAdapter.VH> {

    private List<TaskProject> dataList;
    private Context context;

    public ProjectInfoAdapter(Context context,List<TaskProject> dataList){
        this.dataList = dataList;
        this.context = context;
    }


    public void loadData(List<TaskProject> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }



    @Override
    public void onBindViewHolder(@NonNull ProjectInfoAdapter.VH holder, @SuppressLint("RecyclerView") int position) {
        TextView tv_position = holder.getView(R.id.tv_position);
        TextView tv_title = holder.getView(R.id.tv_title);//项目名称
        TextView tv_jcmj = holder.getView(R.id.tv_jcmj);//建成面积
        TextView tv_tzze = holder.getView(R.id.tv_tzze);//投资总额
        TaskProject data = dataList.get(position);
        tv_position.setText((position+1)+"");
        tv_title.setText(data.getpName());
        tv_jcmj.setText(data.getpJsgm()+"亩");
        tv_tzze.setText(data.getPztz()+"万元");
        View list_item = holder.getView(R.id.list_item);
        list_item.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(onItemListener!=null)onItemListener.onItemLongClick(position);
                return true;
            }
        });
        list_item.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(onItemListener!=null)onItemListener.onItemClick(position);
            }
        });
    }

    public interface OnItemListener{
        void onItemClick(int position);
        void onItemLongClick(int position);
    }
    private OnItemListener onItemListener;

    public OnItemListener getOnItemListener() {
        return onItemListener;
    }

    public void setOnItemListener(OnItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    @Override
    public int getItemCount() {
        if (dataList != null) {
            return dataList.size();
        }
        return 0;
    }

    protected static class VH extends RecyclerView.ViewHolder   {

        private View view;


        public VH(View itemView) {
            super(itemView);
            view = itemView;
        }

        public <T extends View> T getView(int id) {
            return (T) view.findViewById(id);
        }
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_project_item, parent, false);
        return new VH(view);
    }
}
